package fun.xinnix.controller;

import cn.hutool.core.util.RandomUtil;
import entity.SysUser;
import fun.xinnix.entity.Result;
import fun.xinnix.service.SysUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/sysUser")
public class SysUserController {
    @Resource
    private SysUserService sysUserService;

    /**
     * 查询
     *
     * @param userId
     * @return
     */
    @GetMapping("/getUser")
    public Result getUser(Long userId) {
        return Result.success(sysUserService.getById(userId));
    }

    /**
     * 保存
     *
     * @param entity
     * @return
     */
    @PostMapping("/createOrUpdate")
    public Result create(@RequestBody SysUser entity) {
        boolean result = sysUserService.saveOrUpdate(entity);
        if (result) {
            return Result.success(entity);
        }
        return Result.error();
    }

    @PostMapping("/saveBatch")
    public Result saveBatch() {
        for (int i = 0; i < 100; i++) {
            List<SysUser> userList = new ArrayList<>(10000);
            for (int j = 0; j < 10000; j++) {
                SysUser sysUser = new SysUser();
                sysUser.setUserName(RandomUtil.randomString(6));
                sysUser.setPassword(RandomUtil.randomString(10));
                sysUser.setScore(RandomUtil.randomInt(1000));
                sysUser.setRoleId(12L);
                userList.add(sysUser);
            }
            sysUserService.saveBatch(userList);
        }
        return Result.success();
    }
}
